<script setup lang="ts">
import { EyeFilled, HeartFilled, LikeFilled } from '@ant-design/icons-vue'
import AppSidebar from '~/components/AppSidebar.vue'
import type { HomeGETParams } from '@/api/article'
import { getHomeList, getHomeListClient, getTagsList } from '@/api/article'
import defaultImg from '@/assets/image/default.jpg'
import headerImg from '@/assets/image/header.png'
import storage from '@/utils/storage'
import dayjs from 'dayjs'
useHead({
  title: '鹿鸣博客-专注全栈技术分享',
  meta: [
    { charset: 'utf-8' },
    { name: 'viewport', content: 'width=device-width, initial-scale=1, maximum-scale=1' },
  ],
  link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
})
const activeTab = ref('latest')
const _params = {
  isHot: 0,
  isTop: 0,
  page: 1,
  size: 10,
}
// 轮播图数据
const carouselItems = [
  {
    id: 1,
    title: 'Nuxt 4 全面升级指南',
    desc: '探索 Nuxt 4 的新特性和最佳实践',
    image: 'https://picsum.photos/1200/400?random=1',
  },
  {
    id: 2,
    title: 'Vue 3 性能优化技巧',
    desc: '让你的 Vue 应用飞起来',
    image: 'https://picsum.photos/1200/400?random=2',
  },
]

// 文章列表数据
const articles = ref<any>([])
const _total = ref<number>(0)
// 热门标签
const _hotTags = ref<any>([])

// 推荐阅读
const _recommendedArticles = [
  { id: 1, title: '前端性能优化指南', views: 2341 },
  { id: 2, title: 'JavaScript 设计模式', views: 1892 },
  { id: 3, title: 'Web 安全防护实践', views: 1654 },
]
// 分页获取文章列表
const getArticles = async (params: HomeGETParams) => {
  const { data } = await getHomeList(params)
  console.log('获取到的数据data', data?.value)
  const res: any = data?.value
  if (res.code === 200) {
    _total.value = res.data.total
    articles.value = res.data.articles
  }
}
// 客户端获取数据
const getArticlesClient = async (params: HomeGETParams) => {
  const res: any = await getHomeListClient(params)
  console.log('获取到的数据res', res)
  if (res.code === 200) {
    _total.value = res.data.total
    articles.value = [...articles.value, ...res.data.articles]
  }
}
// 点击tab切换
const tabChange = (tab: string) => {
  activeTab.value = tab
  if (tab === 'latest') {
    _params.isHot = 0
    _params.isTop = 0
  } else if (tab === 'hot') {
    _params.isHot = 1
    _params.isTop = 0
  } else if (tab === 'featured') {
    _params.isHot = 0
    _params.isTop = 1
  }
  _total.value = 0
  articles.value = []
  getArticlesClient(_params)
}

// 页面初始化时请求数据
getArticles(_params)
// 获取标签
getTagsList().then((res: any) => {
  console.log('获取到的标签数据res', res.data.value)

  const result = res.data.value
  if (result.code === 200) {
    _hotTags.value = result.data
    storage.set('hotTags', _hotTags.value)
  }
})
// 渲染背景图片
const renderCoverImg = (article: any) => {
  if (article.articleCover) {
    return article.articleCover
  }
  return defaultImg
}
// 添加获取空状态消息的方法
const getEmptyStateMessage = () => {
  switch (activeTab.value) {
    case 'hot':
      return '暂时没有热门文章，去看看最新的内容吧！'
    case 'featured':
      return '暂时没有精选文章，去看看最新的内容吧！'
    default:
      return '还没有发布任何文章，敬请期待！'
  }
}
</script>

<template>
  <div class="blog-home">
    <div class="container">
      <!-- 左侧主内容区 -->
      <div class="main-content">
        <!-- 轮播图 -->
        <div class="carousel-section">
          <a-carousel autoplay>
            <div v-for="item in carouselItems" :key="item.id" class="carousel-item">
              <img :src="item.image" :alt="item.title" />
              <div class="carousel-overlay">
                <h2 class="carousel-title">{{ item.title }}</h2>
                <p class="carousel-desc">{{ item.desc }}</p>
              </div>
            </div>
          </a-carousel>
        </div>

        <!-- Tab 切换 -->
        <div class="tabs-section">
          <div class="tabs">
            <button
              :class="['tab-item', { active: activeTab === 'latest' }]"
              @click="tabChange('latest')"
            >
              最新发布
            </button>
            <button
              :class="['tab-item', { active: activeTab === 'hot' }]"
              @click="tabChange('hot')"
            >
              热门推荐
            </button>
            <button
              :class="['tab-item', { active: activeTab === 'featured' }]"
              @click="tabChange('featured')"
            >
              精选文章
            </button>
          </div>
        </div>

        <!-- 文章列表 -->
        <div v-if="articles.length > 0" class="articles-list">
          <article
            v-for="article in articles"
            :key="article.id"
            class="article-item"
            @click="navigateTo(`/article/${article.id}`)"
          >
            <div class="article-cover">
              <img :src="renderCoverImg(article)" :alt="article.title" />
              <span
                v-for="value in article?.categoryId?.split(',')"
                :key="value"
                class="article-category"
                >{{ _hotTags.find((tag: any) => +tag.id === +value)?.name }}</span
              >
            </div>
            <div class="article-info">
              <h3 class="article-title">{{ article.title }}</h3>
              <p class="article-excerpt">{{ article.content }}</p>
              <div class="article-meta">
                <div class="author-info">
                  <img :src="headerImg" :alt="article?.title" class="avatar" />
                  <span class="author-name">秋水无痕</span>
                  <span class="separator">•</span>
                  <span class="date">{{ dayjs(article.updateTime).format('MM/DD/YYYY') }}</span>
                </div>
                <div class="stats">
                  <span class="stat-item">
                    <EyeFilled />
                    {{ article.readCounts }}
                  </span>
                  <span class="stat-item">
                    <HeartFilled />
                    {{ article.favoriteCount }}
                  </span>
                  <span class="stat-item">
                    <LikeFilled />
                    {{ article.likeCount }}
                  </span>
                </div>
              </div>
            </div>
          </article>
        </div>
        <!-- 精美的空状态组件 -->
        <EmptyState
          v-else
          title="暂无文章"
          :description="getEmptyStateMessage()"
          :button-text="activeTab !== 'latest' ? '查看最新文章' : undefined"
          @button-click="tabChange('latest')"
        />

        <div v-if="articles.length < _total" class="load-more">
          <a-button size="large">加载更多</a-button>
        </div>
      </div>

      <!-- 右侧边栏 -->
      <AppSidebar />
    </div>
  </div>
</template>

<style scoped>
.blog-home {
  padding: 24px 0;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  gap: 24px;
}

/* 左侧主内容区 */
.main-content {
  flex: 1;
  max-width: calc(100% - 344px);
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 轮播图 */
.carousel-section {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.carousel-item {
  position: relative;
  height: 280px;
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.7), transparent);
  padding: 24px;
  color: white;
}

.carousel-title {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px 0;
}

.carousel-desc {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

/* Tab 切换 */
.tabs-section {
  background: white;
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.tabs {
  display: flex;
  gap: 12px;
}

.tab-item {
  flex: 1;
  padding: 10px 20px;
  border: none;
  background: #f7fafc;
  color: #4a5568;
  font-size: 14px;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
}

.tab-item:hover {
  background: #e2e8f0;
  color: #667eea;
}

.tab-item.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

/* 文章列表 */
.articles-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.article-item {
  background: white;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  gap: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s;
  cursor: pointer;
}

.article-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.article-cover {
  position: relative;
  width: 240px;
  height: 160px;
  flex-shrink: 0;
  border-radius: 8px;
  overflow: hidden;
}

.article-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.article-item:hover .article-cover img {
  transform: scale(1.1);
}

.article-category {
  position: absolute;
  top: 12px;
  left: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.article-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.article-title {
  font-size: 20px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 12px 0;
  line-height: 1.4;
}

.article-excerpt {
  font-size: 14px;
  color: #718096;
  line-height: 1.6;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}

.author-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #a0aec0;
}

.avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
}

.author-name {
  font-weight: 600;
  color: #4a5568;
}

.separator {
  color: #cbd5e0;
}

.stats {
  display: flex;
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: #a0aec0;
}

.stat-item .icon {
  width: 16px;
  height: 16px;
}

/* 加载更多 */
.load-more {
  text-align: center;
  padding: 20px 0;
}

.load-more :deep(.ant-btn) {
  height: 44px;
  padding: 0 48px;
  border-radius: 8px;
  font-weight: 600;
  border: 2px solid #e2e8f0;
}

.load-more :deep(.ant-btn:hover) {
  border-color: #667eea;
  color: #667eea;
}

/* 右侧边栏 */
.sidebar {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.sidebar-widget {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.widget-title {
  font-size: 18px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 16px 0;
  padding-bottom: 12px;
  border-bottom: 2px solid #f7fafc;
  position: relative;
}

.widget-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 40px;
  height: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 热门标签 */
.tags-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-item {
  padding: 6px 14px;
  background: #f7fafc;
  border-radius: 6px;
  font-size: 13px;
  color: #4a5568;
  cursor: pointer;
  transition: all 0.3s;
}

.tag-item:hover {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: translateY(-2px);
}

.tag-item em {
  font-style: normal;
  opacity: 0.7;
  font-size: 12px;
}

/* 广告位 */
.ad-widget {
  background: #f7fafc;
}

.ad-placeholder {
  width: 100%;
  height: 250px;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  border: 2px dashed #cbd5e0;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #a0aec0;
}

.ad-placeholder p {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.ad-placeholder span {
  font-size: 14px;
}

/* 推荐阅读 */
.recommended-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.recommended-item {
  display: flex;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s;
  text-decoration: none;
}

.recommended-item:hover {
  background: #f7fafc;
}

.rank {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  background: #e2e8f0;
  color: #718096;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

.rank.top {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.recommended-info {
  flex: 1;
}

.recommended-title {
  font-size: 14px;
  color: #2d3748;
  margin: 0 0 4px 0;
  font-weight: 600;
  line-height: 1.4;
}

.recommended-views {
  font-size: 12px;
  color: #a0aec0;
  display: flex;
  align-items: center;
  gap: 4px;
}

.recommended-views .icon {
  width: 14px;
  height: 14px;
}
/* 在现有样式基础上添加空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4rem 2rem;
  text-align: center;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  margin-top: 16px;
}

.empty-state__illustration {
  margin-bottom: 1.5rem;
  opacity: 0.8;
}

.empty-state__title {
  color: #1a202c;
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
}

.empty-state__description {
  color: #718096;
  font-size: 1rem;
  max-width: 400px;
  line-height: 1.6;
  margin-bottom: 1.5rem;
}

.empty-state__button {
  padding: 0.75rem 1.5rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.empty-state__button:hover {
  transform: translateY(-2px);
}
/* 响应式 */
@media (max-width: 968px) {
  .container {
    flex-direction: column;
  }

  .main-content {
    max-width: 100%;
  }

  .sidebar {
    width: 100%;
  }

  .article-item {
    flex-direction: column;
  }

  .article-cover {
    width: 100%;
    height: 200px;
  }
}
</style>
